import com.mysql.jdbc.jdbc2.optional.MysqlDataSource;

import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Scanner;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 石方旭
 * Date: 2022-07-16
 * Time: 11:04
 */
public class TestJDBC {

    //"jdbc:mysql://127.0.0.1:3306/java104?characterEncoding=utf8&useSSL=false"
    public static void main(String[] args) throws SQLException {

        //建立数据源
        DataSource dataSource = new MysqlDataSource();
        //设置 url user password
        ((MysqlDataSource)dataSource).setUrl("jdbc:mysql://127.0.0.1:3306/java104?characterEncoding=utf8&useSSL=false");
        ((MysqlDataSource)dataSource).setUser("root");
        ((MysqlDataSource)dataSource).setPassword("123456");



        //建立连接
        Connection connection = dataSource.getConnection();

        Scanner scan = new Scanner(System.in);
        int num = scan.nextInt();
        Scanner scan1 = new Scanner(System.in);
        String str = scan1.next();

        //构建sql
        String sql = "insert into student values(?,?)";

        PreparedStatement statement =connection.prepareStatement(sql);

        statement.setInt(1,num);
        statement.setString(2,str);

        //执行sql
        int n = statement.executeUpdate();

        System.out.println("n = " + n);

        //关闭资源
        statement.close();
        connection.close();


    }


}
